package F21AS_Freddy.AFJ_Taxikiosk.src;

import java.util.regex.Pattern;

/**
 * This class used to define properties of each passenger.
 * @author Freddy Wong
 * F21AS Course Work Stage2 @ Year 2015
 */

public class Passenger implements Comparable <Passenger> {
	
	private String bookingName;
	private String bookingRef;
	private int numberOfPassenger;
	private int kiosk;
	
	public Passenger(String bookingName, String bookingRef, int numberOfPassenger, int kiosk) {

		/**
		 * Define Java regular expression to match booking reference
		 */
		Pattern p = Pattern.compile("AFJ-[0-9]{8}");
		boolean patternMatch = p.matcher(bookingRef).matches();

		/**
		 * Validation check to ensure all variable are in range
		 */
		if (bookingRef.length() == 12 && patternMatch && bookingName.length() > 2
				&& bookingName.length() < 20 && numberOfPassenger > 0 && numberOfPassenger < 8) {

			this.bookingName = bookingName;
			this.bookingRef = bookingRef;
			this.numberOfPassenger = numberOfPassenger;
			this.kiosk = kiosk;
			
		} else {
			
			throw new IllegalStateException(
					"Invalid booking name or booking reference or number of passengers "
							+ "Valid booking references begin with 'SA-' followed by 8 digits. "
							+ "Valid booking names are 2 - 30 characters in length."
							+ "Number of Passenger cannot exceed 6 persons");
		}

	}


	public String getBookingName() {
		return bookingName;
	}


	public String getBookingRef() {
		return bookingRef;
	}


	public int getNumberOfPassenger() {
		return numberOfPassenger;
	}
	
	public int getAssignedKiosk() {
		return kiosk;
	}
	
	public void assignKiosk(int kiosk) {
		this.kiosk = kiosk;
	}
	
	
	public String getPassengerDetails() {
		
		String report = "";
		report += String.format("%-7s", getAssignedKiosk());
		report += String.format("%-19s", getBookingRef());
		report += String.format("%-20s", getBookingName());
		report += String.format("%-20s", getNumberOfPassenger());
		report += "\n";
		return report;
	}
	
	/**
	 * @return string representation of the Passenger in the form
	 *         "Full Name, Booking Ref" e.g. "Joe Blogs, SA-12345678"
	 */
	public String toString() {
		return this.getBookingName() + "," + this.getBookingRef();
	}

	/**
	 * As passengers are uniquely identified by booking reference (duplicates are not
	 * allowed) we use the booking reference as the comparator
	 */
	public int compareTo(Passenger otherPassenger) {
		return (this.getBookingRef().compareTo(otherPassenger.getBookingRef()));
	}

	/**
	 * Compare passenger's unique booking reference number
	 */
	public boolean equals(Object other) {
		if(other instanceof Passenger) {
			Passenger otherPassenger = (Passenger) other;
			if(bookingRef.equals(otherPassenger.bookingRef)) {
				return true;
			}
		}

		return false;
	}
	
	/**
	 * return the hash of passenger's unique booking reference number
	 */
	public int hashCode() {
		return bookingRef.hashCode();
	}

} // End Of Passenger Class
